//
//  HRStatusPictureView.swift
//  microBlog
//
//  Created by HR_W on 16/5/25.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit

private let pictureMargin: CGFloat = 3
private let pictureCellID = "pictureCellID"

private let maxWidth = ScreenWidth - 2 * margin

private let itemWidth = (maxWidth - 2 * pictureMargin) / 3

class HRStatusPictureView: UICollectionView {

    
    var pictures: [HRPicture]?{
    
        didSet {
 
            let pSize = calculatePictureViewSize()
            
            self.snp_updateConstraints { (make) in
                
                make.size.equalTo(pSize)
            }
            
            reloadData()
        }
    
    }
    
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = pictureMargin
        layout.minimumInteritemSpacing = pictureMargin
        
        super.init(frame: frame, collectionViewLayout: layout)
        
        dataSource = self
        //设置不能响应点击状态栏快速回到的 手势
        scrollsToTop = false
        registerClass(HRStatusPictureCell.self, forCellWithReuseIdentifier: pictureCellID)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func calculatePictureViewSize() -> CGSize {
    
        let imageCount = pictures?.count ?? 0
        
        if imageCount == 0 {
            
            return CGSizeZero
        }
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        if imageCount == 1 {
            let imageSize = CGSizeMake(100, 150)
            
            layout.itemSize = imageSize
            
            return imageSize
        }
        
        if imageCount == 4 {
            let w = itemWidth * 2 + pictureMargin
            return CGSizeMake(w, w)
            
        }
        
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
    
        let row = CGFloat((imageCount - 1) / 3) + 1
        
        let maxHeight = row * itemWidth + (row - 1) * pictureMargin
        
        return CGSizeMake(maxWidth, maxHeight)
    }
    
}



extension HRStatusPictureView: UICollectionViewDataSource {

    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictures?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellID, forIndexPath: indexPath) as! HRStatusPictureCell
        
        cell.picture = pictures![indexPath.item]
        
        return cell
        
    }

}


class HRStatusPictureCell: UICollectionViewCell {

    var picture: HRPicture? {
    
        didSet {
        
            let url = NSURL(string: picture?.thumbnail_pic ?? "")
            imageView.sd_setImageWithURL(url)
            
        }
    
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        contentView.addSubview(imageView)
        
        imageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView.snp_edges)
        }
    
    }
    
    
    private lazy var imageView: UIImageView = {
    
        let iv = UIImageView()
        iv.contentMode = .ScaleAspectFill
        
        iv.clipsToBounds = true
        
        return iv
    
    
    }()
}
